package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行DQL语句
 */
public class JDBCDemo4 {
    public static void main(String[] args) {
        try (
                Connection connection = DBUtil.getConnection()
        ){
            Statement statement = connection.createStatement();
            /*
                SELECT id,name,age,gender,salary
                FROM teacher
             */
            String sql = "SELECT id, name, age, gender, salary "+
                         "FROM teacher";
            /*
                ResultSet executeQuery(String sql)
                专门用于执行DQL语句的方法，返回值为查询结果集
             */
            ResultSet rs = statement.executeQuery(sql);
            /*
                ResultSet表示查询结果集
                boolean next()
                让结果集向下移动一条记录并表示它，如果结果集有这条记录则返回true
             */
            while(rs.next()){
                int id = rs.getInt("id");//获取这一条记录"id"字段的值，由于该字段是整数类型，因此调用getInt
                String name = rs.getString("name");
                int age = rs.getInt("age");
                String gender = rs.getString("gender");
                int salary = rs.getInt("salary");
                System.out.println(id+","+name+","+age+","+gender+","+salary);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
